var Sozlukte = {

	onLoad: function() 
	{
		this.initialized = true;
		document.getElementById("contentAreaContextMenu").addEventListener(
		   "popupshowing",Sozlukte.onContextMenu,false);
	},
  
	onContextMenu: function()
	{
		var focusedWindow = document.commandDispatcher.focusedWindow;
		var strSearch = focusedWindow.getSelection().toString().replace(/^\s*|\s*$/g,'');
		var nLength = strSearch.length;

		if( nLength == 0) 
			{
				// Nothing or too long text selected -> hide menu options
				document.getElementById("sozlukteSeperator").hidden = true;
				document.getElementById("sozlukteMenuItem").hidden = true;
			}
		else 
			{
				var bundle = document.getElementById("sozlukteLocale");

				// enable menu options
				document.getElementById("sozlukteSeperator").hidden = false;
				document.getElementById("sozlukteMenuItem").hidden = false;
				
				// display selected text, too
				if(nLength <= 25)			
					document.getElementById("sozlukteMenuItem").label = bundle.getString("translate") + ": \"" + strSearch + "\"";
				else
					document.getElementById("sozlukteMenuItem").label = bundle.getString("translate") + ": \"" + strSearch.substring(0,25)+"...\"";				
			}
	},

	translate: function() 
	{
		// what a nice command:
		var strSearch = document.commandDispatcher.focusedWindow.getSelection().toString().replace(/^\s*|\s*$/g,'');
		var bundle = document.getElementById("sozlukteLocale");
		var DICTIONARY_URL = bundle.getString("DICTIONARY_URL");

		var strAddr = DICTIONARY_URL + encodeURIComponent(strSearch);

		//window.open( strAddr, 'sozlukteWin');
		// #if DEBUG
		//alert(getBrowser().contentDocument.location.href);
		// #endif  :)
		if(getBrowser().contentDocument.location.href.indexOf("sozlukte.com") < 1)
			getBrowser().selectedTab = getBrowser().addTab(strAddr);
		else
			getBrowser().loadURI(strAddr);
	},
};

window.addEventListener("load", function(e) { Sozlukte.onLoad(e); }, false);